package com.leve.control.service;

import com.leve.control.domain.SetPermission;
import com.leve.control.mapper.SetPermissionMapper;
import com.leve.control.util.MD5Util;
import com.leve.control.util.UuidUtil;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author whongyu
 * @create by 2019/6/17
 */
@Service
public class SetPermissionService {

    @Resource
    private SetPermissionMapper setPermissionMapper;

    public List<SetPermission> findSetPermission(SetPermission setPermission){
        Map<String,Object> map = new HashMap<>();
        map.put("userName",setPermission.getUserName());
        map.put("userPermission",setPermission.getUserPermission());
        return setPermissionMapper.findAll(map);
    }

    public int addSetPermission(SetPermission setPermission){
        String s = MD5Util.fromStringToMD5(setPermission.getUserPass());
        setPermission.setUserPass(s);
        setPermission.setSetPId(UuidUtil.getUUID());
        setPermission.setCreateTime(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
        return setPermissionMapper.add(setPermission);
    }

    public int deleteSetPermission(String setPId){
        return setPermissionMapper.remove(setPId);
    }

    public int editSetPermission(SetPermission setPermission){
        SetPermission s = setPermissionMapper.findSetPermById(setPermission.getSetPId());
        if (!s.getUserPass().equals(s.getUserPass())){
            String s1 = MD5Util.fromStringToMD5(setPermission.getUserPass());
            setPermission.setUserPass(s1);
        }
        return setPermissionMapper.update(setPermission);
    }
}
